package com.zhuiyun.project.api.filemanage.controller;

import com.zhuiyun.project.api.filemanage.entity.FileManage;
import com.zhuiyun.project.api.filemanage.service.FileManageService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.response.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;

/**
 * @ClassName FileManageController
 * @Description TODO
 * @Author kdj
 * @Date 2023/6/27 9:20
 **/
@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileManageController {

    @Autowired
    FileManageService fileManageService;


    /**
     * @param multipartRequest
     * @Author kdj
     * @Description 用户图片 MultipartRequest
     * @Date 2023/6/27 9:22
     * @Return java.lang.Integer
     */
    @RequestMapping("/File")
    public Integer uploadFile(MultipartRequest multipartRequest) {
        return fileManageService.uploadFile(multipartRequest);
    }

    /**
     * @param file
     * @Author kdj
     * @Description 用户图片
     * @Date 2023/6/27 9:22
     * @Return java.lang.Integer
     */
    @RequestMapping("/isFile")
    public CommonResult uploadFile(MultipartFile file) {
        return CommonResult.ok(fileManageService.uploadFile(file));
    }


    /**
     * 根据id获取视频
     *
     * @param response
     * @param id
     */
    @GetMapping("/getVideoById")
    public void getVideoById(HttpServletResponse response, Integer id) throws CommonException, FileNotFoundException {
        fileManageService.getVideoById(response, id);
    }


    /**
     * 视频上传
     *
     * @param multipartRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadFile")
    public CommonResult uploadFiles(MultipartRequest multipartRequest) throws Exception {
        MultipartFile file = multipartRequest.getFile("file");
        String projectName = "other";
        FileManage fileManage = fileManageService.saveFileToSystem(file, projectName);
        if (fileManage.getId() > 0) {
            return CommonResult.ok(fileManage.getId());
        }
        return CommonResult.error();
    }

}
